package com.avic.system.controller;

import com.avic.system.common.constant.LocalCacheConstant;
import com.avic.system.common.convertor.SystemMenuConvertor;
import com.avic.system.common.util.lang.ServiceCallBack;
import com.avic.system.controller.info.MenuTreeInfo;
import com.avic.system.controller.result.GetMenuTreeRes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/system/menu")
@Api(tags = "系统菜单", description = "获取菜单树接口")
public class SystemMenuController extends AbstractFacadeImpl {

    @ApiOperation(value = "获取菜单树数据", notes = "获取菜单树数据")
    @GetMapping(value = "menuTree")
    public GetMenuTreeRes getMenuTreeData() {
        log.info("获取菜单树数据--getMenuTreeData");
        final GetMenuTreeRes result = new GetMenuTreeRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
            }

            @Override
            public void doService() throws Exception {
                if (null != LocalCacheConstant.MENU_TREE && LocalCacheConstant.MENU_TREE.size() > 0) {
                    log.info("从缓存中获取菜单树数据");
                    result.setMenuTree(LocalCacheConstant.MENU_TREE);
                } else {
                    log.info("从DB中获取菜单树数据");
                    List<MenuTreeInfo> menuTreeInfos = SystemMenuConvertor.systemMenuTreeListConInfo(systemMenuService.getMenuTreeData());
                    result.setMenuTree(menuTreeInfos);
                    // 并且设置缓存
                    LocalCacheConstant.MENU_TREE.addAll(menuTreeInfos);
                }
                result.setSuccess(true);
            }

        });
    }
}
